package eval

import (
	"showen/lex"
	"showen/obj"
	"showen/parse"
	"testing"
)

func TestLoadIntegration(t *testing.T) {
	tests := []struct {
		input    string
		expected string
	}{
		{
			input:    `(load "load_test.scm")`,
			expected: "1764",
		},
		{
			input:    "x",
			expected: "42",
		},
		{
			input:    "(square 5)",
			expected: "25",
		},
	}

	env := obj.NewEnvironment()
	RegisterBuiltinProcedures(env)

	for _, tt := range tests {
		t.Run(tt.input, func(t *testing.T) {
			lexer := lex.NewLexer(tt.input)
			tokens := lexer.ReadTokens()

			parser := parse.NewParser(tokens)
			exprs, err := parser.ParseProgram()
			if err != nil {
				t.Fatalf("ParseProgram() error = %v", err)
			}

			var result obj.Object
			for _, expr := range exprs {
				result, err = EvalExpression(expr, env)
				if err != nil {
					t.Fatalf("EvalExpression() error = %v", err)
				}
			}

			if result.Inspect() != tt.expected {
				t.Errorf("For input %q, expected %q, got %q", tt.input, tt.expected, result.Inspect())
			}
		})
	}
}
